package com.qikecn.utils;

import java.util.List;

import cn.geekapp.utils.StringUtils;

import com.jfinal.plugin.activerecord.Model;

/**
 * 手机号工具类
 * @author wangst
 *
 */
public class PhoneUtil {
	
	/**
	 * 隐藏手机号中间四位
	 * @param phone
	 * @return
	 */
	public static final String replacePhoneMidFourStar(String phone){
		if(phone == null || !phone.matches("[0-9]{11}")){
			return "";
		}
		return replaceStar(phone, 3, 7);
	}
	
	/**
	 * 替换星号
	 * @param str
	 * @param start
	 * @param end
	 * @return
	 */
	public static final String replaceStar(String str, int start, int end){
		if(str == null || str.equals("")){
			return "";
		}
		if(start > end){
			return "";
		}
		if(str.length()-1 < end){
			return "";
		}
		StringBuffer buffer = new StringBuffer(str);
		String star = "";
		for(int i=start; i<end; i++){
			star += "*";
		}
		buffer.replace(start, end, star);
		return buffer.toString();
	}
	
	public static void main(String[] args) {
		System.out.println(PhoneUtil.replacePhoneMidFourStar("13266666271"));
	}
	
	
	/**
	 * 昵称为空时，设置为用户手机号
	 * @param list
	 */
	public static void setUsernameIfNull(List<Model> list){
		if(list != null){
			for(Model model : list){
				if(StringUtils.isEmpty(model.getStr("username"))){
					model.put("username", model.getStr("phone"));
				}
			}
		}
	}
	
}
